package com.project.weimi.activity;

import java.util.ArrayList;
import java.util.List;

import com.bright.multiphoto.AlbumActivity;
import com.bright.multiphoto.Base64Util;
import com.project.weimi.R;
import com.project.weimi.utils.BaseUtils;
import com.project.weimi.widget.SelectPicPopupWindow;
import com.project.weimi.widget.ToastMessage;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

/** 意见反馈 */
public class FeedBackActivity extends BaseActivity {

	private static final int TAKE_PICTURE = 0x000001;

	private EditText feed_content;

	private List<Bitmap> listBitmap = null;
	private GridView gridview;
	private GridAdapter adapter;
	private SelectPicPopupWindow menuWindow = null;
	
	private Bitmap add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentViewWithBar(R.layout.activity_feed_back, "意见反馈", true, "提交");

		feed_content = (EditText) this.findViewById(R.id.feed_content);
		gridview = (GridView) this.findViewById(R.id.gridview);

		bar.getRightBar().setOnClickListener(l);

		initGridView();
	}

	private void initGridView() {
		// TODO Auto-generated method stub
		listBitmap = new ArrayList<Bitmap>();
		add = BaseUtils.readBitMap(FeedBackActivity.this, R.drawable.icon_feed_add);
		listBitmap.add(add);
		
		adapter = new GridAdapter(FeedBackActivity.this, listBitmap);
		gridview.setAdapter(adapter);
		
		gridview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if(position == listBitmap.size() - 1){
					menuWindow = new SelectPicPopupWindow(FeedBackActivity.this, itemsOnClick);
					// 设置layout在PopupWindow中显示的位置
					menuWindow.showAtLocation(FeedBackActivity.this.findViewById(R.id.infomation),
							Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
				}
			}
		});
		
	}

	OnClickListener l = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			default:
				// 处理提交
				ToastMessage.showToast(FeedBackActivity.this, feed_content.getText().toString());
				
				int count = listBitmap.size() - 1;
				for (int i = 0; i < count; i++) {
					String baseString = Base64Util.compress(listBitmap.get(i));
					Log.e(FeedBackActivity.class.getSimpleName(), baseString.length() + "");
				}
				

				// 显示反馈结果 AlertDialog
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub

						View view = LayoutInflater.from(FeedBackActivity.this)
								.inflate(R.layout.activity_feed_back_result, null);
						TextView msg = (TextView) view.findViewById(R.id.feed_msg);
						TextView ok = (TextView) view.findViewById(R.id.feed_ok);
						msg.setText("反馈成功了!赞~");
						final AlertDialog dialog = new AlertDialog.Builder(FeedBackActivity.this).setView(view)
								// 不关闭写 false 关闭写 true
								.setCancelable(false).create();
						dialog.show();

						ok.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								dialog.cancel();
								finish();
							}
						});
					}
				}, 2000);
				break;
			}
		}
	};

	OnClickListener itemsOnClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_take_photo:// 拍照

				Log.e("TAG", "" + BaseUtils.getAPIVersion());

				if (BaseUtils.getAPIVersion() < 22) {
					takePhoto();
					menuWindow.dismiss();
				} else if (ContextCompat.checkSelfPermission(FeedBackActivity.this,
						Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
					ActivityCompat.requestPermissions(FeedBackActivity.this,
							new String[] { Manifest.permission.CAMERA }, 7);

				} else {
					takePhoto();
					menuWindow.dismiss();
				}

				break;
			case R.id.btn_pick_photo:// 手机相册

				Intent intent = new Intent(FeedBackActivity.this, AlbumActivity.class);
				startActivityForResult(intent, 0);

				menuWindow.dismiss();
				break;
			}
		}
	};

	public void takePhoto() {
		Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(openCameraIntent, TAKE_PICTURE);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case TAKE_PICTURE:
			if (resultCode == RESULT_OK) {
				Bitmap bm = (Bitmap) data.getExtras().get("data");
				listBitmap.add(bm);
				
				if(listBitmap.contains(add)){
					listBitmap.remove(add);
					listBitmap.add(listBitmap.size(), add);
				}

				Log.e("TAG", "----" + listBitmap.size());
				
				adapter = new GridAdapter(FeedBackActivity.this, listBitmap);
				gridview.setAdapter(adapter);
			}
			break;
		case 0:
			if (resultCode == 0) {
				List<String> listString = data.getStringArrayListExtra("list");
				Log.e("TAG", listString.toString());
				for (int i = 0; i < listString.size(); i++) {
					listBitmap.add(BaseUtils.readBitMap(FeedBackActivity.this, listString.get(i)));
				}
				
				if(listBitmap.contains(add)){
					listBitmap.remove(add);
					listBitmap.add(listBitmap.size(), add);
				}
				
				Log.e("TAG", "----" + listBitmap.size());
				
				adapter = new GridAdapter(FeedBackActivity.this, listBitmap);
				gridview.setAdapter(adapter);
			}
			break;
		}
	}

	class GridAdapter extends BaseAdapter {
		private Context context;
		private List<Bitmap> listBitmap = null;

		public GridAdapter(Context context, List<Bitmap> listBitmap) {
			super();
			this.context = context;
			this.listBitmap = listBitmap;
		}
		
		public void notifyDataSetChanged(List<Bitmap> listBitmap) {
			// TODO Auto-generated method stub
			if(this.listBitmap != null){
				this.listBitmap.clear();
			} else {
				this.listBitmap = new ArrayList<Bitmap>();
			}
			
			this.listBitmap.addAll(listBitmap);
			GridAdapter.this.notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listBitmap.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listBitmap.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.row_multiphoto_item, null);

				holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
				holder.checkBox1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
				holder.checkBox1.setVisibility(View.GONE);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			LayoutParams params = (LayoutParams) holder.imageView.getLayoutParams();
			params.width = (BaseUtils.getDefaultDisplay(FeedBackActivity.this).width - 60) / 4;
			params.height = (BaseUtils.getDefaultDisplay(FeedBackActivity.this).width - 60) / 4;
			
			holder.imageView.setImageBitmap(listBitmap.get(position));
			holder.imageView.setLayoutParams(params);

			return convertView;
		}

		class ViewHolder {
			ImageView imageView;
			CheckBox checkBox1;
		}
	}

	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
}
